import { createStore } from 'vuex'
import * as types from './action.types'
import * as api from '../api'
import { Payload, State } from './state.types'
export default createStore({
  state: {
    planList: []
  },
  mutations: {
    [types.GET_PLAN_LIST](state: State, payload: Array<Payload>) {
      state.planList = payload
    },
    [types.ADD_PLAN](state: State, payload) {
      state.planList = [...state.planList, payload]
    },
    [types.DELETE_PLAN](state: State, payload: Payload) {
      state.planList = state.planList.filter(item => item._id !== payload._id)
    },
  },
  actions: {
    async [types.GET_PLAN_LIST]({ commit }, payload: Payload) {
      const result = await api.getPlanList()
      commit(types.GET_PLAN_LIST, result)
    },
    async [types.ADD_PLAN]({ commit }, payload: Payload) {
      const result = await api.addPlanList(payload)
      commit(types.ADD_PLAN, payload)
    },
    async [types.DELETE_PLAN]({ commit }, payload: Payload) {
      const result = await api.deletePlanList(payload._id)
      commit(types.DELETE_PLAN, payload)
    },
  },
  modules: {
  }
})
